import React from 'react';
import {inject, observer} from 'mobx-react';
import {WeaLeftRightLayout, WeaLocaleProvider, WeaNewScroll, WeaOrgTree, WeaTools} from 'ecCom';
import {Menu, Tooltip} from 'antd';
import AccessResultList from './accessResult/AccessResultList';
import AccessAuditList from './AccessAuditList';
import ProgramAuditList from './ProgramAuditList';
import ReportList from './report/ReportList';
import GpReportIndex from './gpReport/GpReportIndex';
import ScoreReportIndex from './scoreReport/ScoreReportIndex';
import ProgramList from './ProgramList';
import ProgramView from './programView/ProgramView';
import * as Util from '../../util/Util'

const Item = Menu.Item;
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效
 */
@inject('accessAuditListStore')
@observer
export default class PerformanceIndex extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            cunrrentkey: "1",
            scount: 0,
            auditamount: 0,
            nosetamount: 0,
            isRight: "", //是否开启了绩效管理模块
            hasRight: "",//是否拥有GP_BaseSettingMaint权限
            subcount: 0,
            _selectKey: [],
            _height: Util.getCurrentHeight(),
            time: new Date(),
        }
    }

    componentWillMount() {//一些初始化请求
        WeaTools.callApi('/api/workrelate/common/checkHasRigth', 'GET', {id: 3}).then((data) => {
            if (data.status && data.isRight) {
                //跳转过来的页面，获取参数
                if (Util.getParams("menutype")) {
                    this.setState({
                        cunrrentkey: Util.getParams("menutype")
                    })
                }
                if (Util.getParams("showtype")) {
                    this.props.accessAuditListStore.onSearchChange({
                        showtype: Util.getParams("showtype")
                    })
                }
                if (Util.getParams("type1")) {
                    this.props.accessAuditListStore.onSearchChange({
                        type1: Util.getParams("type1")
                    })
                }
                this.setState({isRight: '1', hasRight: data.hasRight, subcount: data.subcount})
                this.updateNum();
            } else {
                this.setState({isRight: "0"});
            }
        })
    }

    //更新考核评分和方案设置查询数量
    updateNum = () => {
        WeaTools.callApi('/api/workrelate/performance/accessAuditProgramNum', 'GET', {operation: "all"}).then((data) => {
            if (data.api_status) {
                this.setState({scount: data.scount, auditamount: data.auditamount, nosetamount: data.nosetamount});
            }
        });
    }

    handleClick = (e) => {
        if (e.key == 2) {
            this.props.accessAuditListStore.onSearchChange({showtype: "1"})
        }
        this.setState({
            cunrrentkey: e.key,
            time: new Date(),
            _selectKey: []
        });
    }

    onNodeSelect(ckey) {
        this.setState({_selectKey: ckey});
    }

    getMenu() {
        const {cunrrentkey, nosetamount, scount, auditamount, hasRight} = this.state;
        if (hasRight) {
            return <Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@dqc08z`} onClick={this.handleClick}
                         style={{width: 248}}
                         defaultOpenKeys={['1']}
                         selectedKeys={[cunrrentkey]}
                         mode="inline">
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@vf5za2`} key="1" title={getLabel(506161, "考核成绩")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506161, "考核成绩")}</span></Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@1qtkbg`} key="2" title={getLabel(506162, "考核评分")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506162, "考核评分")}</span>
                    {scount > 0 &&
                    <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@2pw6pw`} title={getLabel('509328', '{num}个待评分或审批绩效考核').replace('{num}', scount)}><span
                        className="workrelate-unread">{scount}</span></Tooltip>}
                </Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@vmsvn4`} key="3" title={getLabel(506163, "我的考核方案")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506163, "我的考核方案")}</span></Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@04sp0s`} key="4" title={getLabel(506164, "待审核方案")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506164, "待审核方案")}</span>
                    {auditamount > 0 &&
                    <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@tn933d`} title={getLabel('509329', '{num}个待审批考核方案').replace('{num}', auditamount)}><span
                        className="workrelate-unread">{auditamount}</span></Tooltip>}
                </Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@x0cg7x`} key="5" title={getLabel(506165, "方案设置查询")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506165, "方案设置查询")}</span>
                    {nosetamount > 0 &&
                    <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@tr99tv`} title={getLabel('509330', '{num}个考核方案未设置').replace('{num}', nosetamount)}><span
                        className="workrelate-unread">{nosetamount}</span></Tooltip>}
                </Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@c2vjax`} key="7" title={getLabel(506167, "考核报表")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506167, "考核报表")}</span></Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@lil2n7`} key="8" title={getLabel(506168, "考核执行分析")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506168, "考核执行分析")}</span></Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@ht2jny`} key="9" title={getLabel(506169, "考核结果分析")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506169, "考核结果分析")}</span></Item>
                {/*<Item key="10" title={"考核方案"} style={{height: '30px', lineHeight: "30px"}}><span>考核方案</span></Item>*/}
                {/* <Item key="11" title={getLabel(506170, "模板设置")}><span>{getLabel(506170, "模板设置")}</span></Item> */}
            </Menu>
        } else {
            return <Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@wvwzdc`} onClick={this.handleClick}
                         style={{width: 248}}
                         defaultOpenKeys={['1']}
                         selectedKeys={[cunrrentkey]}
                         mode="inline">
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@sgyrwd`} key="1" title={getLabel(506161, "考核成绩")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506161, "考核成绩")}</span></Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@mt5orf`} key="2" title={getLabel(506162, "考核评分")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506162, "考核评分")}</span>
                    {scount > 0 &&
                    <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@ps1idb`} title={getLabel('509328', '{num}个待评分或审批绩效考核').replace('{num}', scount)}><span
                        className="workrelate-unread">{scount}</span></Tooltip>}
                </Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@ro5v2u`} key="3" title={getLabel(506163, "我的考核方案")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506163, "我的考核方案")}</span></Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@06uw10`} key="4" title={getLabel(506164, "待审核方案")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506164, "待审核方案")}</span>
                    {auditamount > 0 &&
                    <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@t5j4ma`} title={getLabel('509329', '{num}个待审批考核方案').replace('{num}', auditamount)}><span
                        className="workrelate-unread">{auditamount}</span></Tooltip>}
                </Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@ycu1hp`} key="5" title={getLabel(506165, "方案设置查询")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506165, "方案设置查询")}</span>
                    {nosetamount > 0 &&
                    <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@92pmsb`} title={getLabel('509330', '{num}个考核方案未设置').replace('{num}', nosetamount)}><span
                        className="workrelate-unread">{nosetamount}</span></Tooltip>}
                </Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@dpul24`} key="7" title={getLabel(506167, "考核报表")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506167, "考核报表")}</span></Item>
                <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@nqwrjg`} key="9" title={getLabel(506169, "考核结果分析")}
                      style={{height: '30px', lineHeight: "30px"}}><span>{getLabel(506169, "考核结果分析")}</span></Item>
                {/*<Item key="10" title={"考核方案"} style={{height: '30px', lineHeight: "30px"}}><span>考核方案</span></Item>*/}
                {/* <Item key="11" title={getLabel(506170, "模板设置")}><span>{getLabel(506170, "模板设置")}</span></Item> */}
            </Menu>
        }
    }

    chooseUser(e) {
        let hrmId = '', hrmName = undefined;
        if (e.selected) {
            hrmId = e.node.props.id;
            hrmName = e.node.props.name;
        }
        if (hrmId) {
            this.refs["accessMenu" + this.state.cunrrentkey].wrappedInstance.onPersonClick(hrmId, hrmName);
        }
    }

    render() {
        const {cunrrentkey, nosetamount, scount, auditamount, isRight, hasRight, subcount, _height, _selectKey, time} = this.state;
        if (isRight == "") {
            return <div></div>;
        }
        if (isRight == "0") {
            return Util.renderAlertPage('<span style="color:red">' + getLabel(509311, "您的系统未开启绩效管理模块") + '</span>','','','',this.props.ecId || '');
        }
        let menu = this.getMenu();
        let menumap = {
            "1": <AccessResultList ecId={`${this && this.props && this.props.ecId || ''}_AccessResultList@j7qx1w`} key={time} updateNum={this.updateNum} ref="accessMenu1"/>,
            "2": <AccessAuditList ecId={`${this && this.props && this.props.ecId || ''}_AccessAuditList@yc2rxm`} key={time} updateNum={this.updateNum} ref="accessMenu2"/>,
            "3": <ProgramView ecId={`${this && this.props && this.props.ecId || ''}_ProgramView@yb49bv`} key={time} updateNum={this.updateNum} isMy={true} ref="accessMenu3"/>,
            "4": <ProgramAuditList ecId={`${this && this.props && this.props.ecId || ''}_ProgramAuditList@36lxnn`} updateNum={this.updateNum}/>,
            "5": <ProgramList ecId={`${this && this.props && this.props.ecId || ''}_ProgramList@j5pbmo`} updateNum={this.updateNum}/>,
            "7": <ReportList ecId={`${this && this.props && this.props.ecId || ''}_ReportList@5buyzi`} key={time} ref="accessMenu7"/>,
            "8": <GpReportIndex ecId={`${this && this.props && this.props.ecId || ''}_GpReportIndex@ixfavc`}/>,
            "9": <ScoreReportIndex ecId={`${this && this.props && this.props.ecId || ''}_ScoreReportIndex@cjm8j3`}/>,
            // "11": <TemplateSetting />
        }
        return (
            <div className="wea-workrelate-perform-menu wea-workrelate-common-menu">
                <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@q2em9e`}
                    leftCom={<div className="common-menu-title">{menu}
                        {(cunrrentkey == 1 || cunrrentkey == 2 || cunrrentkey == 3 || cunrrentkey == 7 || cunrrentkey == 10) && subcount > 0 &&
                        <div>
                            <div className="common-menu-subtitle">{getLabel(442, "下属")}</div>
                            <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@g9nugm`} height={_height - 300}>
                                <WeaOrgTree ecId={`${this && this.props && this.props.ecId || ''}_WeaOrgTree@3jrvp3`} dataUrl="/api/hrm/base/getHrmResourceTree" rootKey="rootManager"
                                            treeNodeClick={(e) => this.chooseUser(e)} selectedKeys={_selectKey}
                                            onNodeSelect={(ckey) => this.onNodeSelect(ckey)}/>
                            </WeaNewScroll></div>}
                    </div>}
                    leftWidth={248}
                    onCollapse={showLeft => {
                    }}>
                    <div>{menumap[cunrrentkey]}</div>
                </WeaLeftRightLayout>
            </div>
        )
    }
};